package mrHunter.lib;
import java.util.Arrays;
import java.util.Random;

public class MyRandom {
    private Random rand;

    public MyRandom(){
        this.rand=new Random();
    }

    public double randomDouble(double begin, double end){
        return begin+this.rand.nextDouble()*(end-begin);
    }

    public int randomInt(int begin, int end){return begin+this.rand.nextInt(end-begin);}

    public String randomString(){
        StringBuilder res = new StringBuilder();
        int length=rand.nextInt(50);
        for(int i=0;i<length;i++){
            int r=rand.nextInt(122-97+1)+97;
            res.append(r);
        }
        return res.toString();
    }

    public String randomString(int begin, int end){
        StringBuilder res = new StringBuilder();
        int length=randomInt(begin,end);
        for(int i=0;i<length;i++){
            int r=rand.nextInt(122-97+1)+97;
            res.append(r);
        }
        return res.toString();
    }

    public String randomTriangle()
    {
        int a = 0;
        int b = 0;
        int c = 0;
        while(true)
        {
            if(a + b > c && a + c > b && c + b > a)
            {
                return String.valueOf(a)+","+String.valueOf(b)+","+String.valueOf(c);
            }
            a = this.rand.nextInt(100);
            b = this.rand.nextInt(100);
            c = this.rand.nextInt(100);
        }
    }

    public Graph randomGraph(){
        int v=rand.nextInt(50)+2;
        int[][] edges=new int[v][v];
        for(int i=1;i<v;i++){
            for(int j=0;j<i;j++){
                edges[i][j]=rand.nextInt(2);
                edges[j][i]=edges[i][j];
            }
        }
        return new Graph(edges);
    }

    public Graph randomConnectedGraph(){
        Graph res;
        do {
            res=randomGraph();
        }while (!res.isConnected());
        return res;
    }

    public int[] randomIntArray(int type, int type2){
        //type: 1.Random, 2.Ascending Order, 3.Descending Order
        //type2: 1.Repeatable, 2.Not Repeatable
        int len=randomInt(1,20);
        int[] res=new int[len];
        if(1==type2){
            for(int i=0;i<len;i++){
                res[i]=randomInt(-100,100);
            }
        }
        if(2==type2){
            for(int i=0;i<len;){
                res[i]=randomInt(-100,100);
                int j;
                for(j=0;j<i;j++){
                    if(res[j]==res[i]){
                        break;
                    }
                }
                if(j==i) i++;
            }
        }

        if(2==type){
            //Ascending Order
            Arrays.sort(res);
        }
        if(3==type){
            //TODO:Descending Order
        }
        return res;
    }

    public int[] randomIntArray(int type, int type2, int begin, int end){
        //type: 1.Random, 2.Ascending Order, 3.Descending Order
        //type2: 1.Repeatable, 2.Not Repeatable
        int len=randomInt(1,20);
        int[] res=new int[len];
        if(1==type2){
            for(int i=0;i<len;i++){
                res[i]=randomInt(begin,end);
            }
        }
        if(2==type2){
            for(int i=0;i<len;){
                res[i]=randomInt(begin,end);
                int j;
                for(j=0;j<i;j++){
                    if(res[j]==res[i]){
                        break;
                    }
                }
                if(j==i) i++;
            }
        }

        if(2==type){
            //Ascending Order
            Arrays.sort(res);
        }
        if(3==type){
            //TODO:Descending Order
        }
        return res;
    }

    public int[][] randomMatrix(int M, int N,int begin, int end){
        int[][] matrix=new int[M][N];
        for(int i=0;i<M;i++){
            for(int j=0;j<N;j++)
                matrix[i][j]=randomInt(begin,end);
        }
        return matrix;
    }
}
